var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({__proto__: []} instanceof Array && function (d, b) {
            d.__proto__ = b;
        }) ||
        function (d, b) {
            for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        };
    return function (d, b) {
        extendStatics(d, b);

        function __() {
            this.constructor = d;
        }

        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var test = ui.lock.lockUI;
var Handler = Laya.Handler;
var Loader = Laya.Loader;
var BitmapFont = laya.display.BitmapFont;
var TXT = laya.display.Text;
var GameUi = /** @class */ (function (_super) {
    __extends(GameUi, _super);

    function GameUi() {
        var _this = _super.call(this) || this;
        _this.numIndexArr = {
            0: 10,
            1: 7,
            2: 4,
            3: 6,
            4: 11,
            5: 18,
            6: 12,
            7: 13,
            8: 19,
            9: 20,
            10: 14,
            11: 1,
            12: 8,
            13: 15,
            14: 2,
            15: 9,
            16: 16,
            17: 3,
            18: 0,
            19: 17,
            20: 5
        };
        _this.speed = 4;
        _this.guideNum = 0;
        _this.guideLang = {
            1: ["指针到金币时点击屏幕即可击中金币", "击中金币后“福”字会旋转一定的角度", "成功将“福”字旋转180°后福就到啦！"],
            0: ["指针旋转到金币上方时点击屏幕任意区域", "每次击中金币则得分+1,未击中金币则游戏结束", "未击中金币则游戏结束"]
        };
        _this.canGame = false;
        _this.mFontName = "diyFont";
        _this.isInGuide = false;
        _this._isPlay = false;
        _this._clickTime = 0;
        _this.mBitmapFont = new BitmapFont();
        //这里不需要扩展名，外部保证fnt与png文件同名
        _this.mBitmapFont.loadFont("res/font/num.fnt", new Handler(_this, _this.init));
        return _this;
    }

    GameUi.prototype.init = function () {
        this.mBitmapFont.setSpaceWidth(10);
        TXT.registerBitmapFont(this.mFontName, this.mBitmapFont);
        this.numTxt.font = this.mFontName;
        this.game_btn.visible = false;
        this.mouseThrough = true;
        this.giveUp_btn.on(Laya.Event.CLICK, this, this.restart);
        this.guide_box.visible = false;
        this.moduleBg.visible = false;
        this.visible = false;
        Laya.timer.once(700, this, this.resetSize);
        // this.resetSize();
    };

    GameUi.prototype.resetSize = function () {
        this.visible = true;
        this.bg_0.width = Laya.stage.width;
        this.bg_1.width = Laya.stage.width;
        this.bg_2.width = Laya.stage.width;
        this.bg_0.x = (Laya.stage.width - this.bg_0.width) / 2;
        this.bg_1.x = (Laya.stage.width - this.bg_1.width) / 2;
        this.bg_2.x = (Laya.stage.width - this.bg_2.width) / 2;
        var minScale = Math.min(Laya.stage.width / this.main_box.width, Laya.stage.height / this.main_box.height);
        this.main_box.scaleX = minScale;
        this.main_box.scaleY = minScale;
        this.main_box.x = (Laya.stage.width - this.main_box.width * minScale) / 2;
        this.speed = gameData["speed"];
        this.guide_box.x = (Laya.stage.width - this.guide_box.width) / 2;
        this.moduleBg.width = Laya.stage.width;
        this.moduleBg.x = (this.main_box.width - this.moduleBg.width) / 2;
        this.game_btn.x = (Laya.stage.width - this.game_btn.width) / 2;
        if (gameData["guide"]) {
            this.clickUi();
            Laya.stage.on(Laya.Event.CLICK, this, this.clickGuide);
            this.top_img.visible = false;
            this.gameTime_txt.visible = false;
        } else {
            this.top_img.visible = true;
            this.gameTime_txt.visible = true;
        }
        $(".mask").hide();
    };
    /**
     * 开始游戏
     * @param level 游戏难度
     * @param callBack 回调函数
     */
    GameUi.prototype.startGame = function () {
        this._gameState = 2;
        this.gameTime = gameData["GameNums"];
        this._isPlay = false;
        Laya.stage.mouseEnabled = true;
        this._rightNums = 0;
        this.rotation_img.rotation = 0;
        this.boxRand();
        this.setGameState(2);
        this.giveUp_btn.visible = false;
        // this.again_btn.visible=false;
        this.numTxt.text = "0";
        this.gameTime_txt.text = "我的最高分:" + gameData["total"];
        if (!gameData["guide"]) {
            this.game_btn.visible = false;
            Laya.stage.on(Laya.Event.CLICK, this, this.clickUi);
        }
        if (gameData["guide"]) {
            console.log("start guide")
            this.setGameing(true);
        } else {
            gameData["restartBack"] && gameData["restartBack"].apply(this);
        }
    };
    GameUi.prototype.setGameing = function (game) {
        this.canGame = game;
    };
    GameUi.prototype.restart = function () {
        if (gameData["guide"]) {
            console.log("restart guide")
            this.setGameing(true);
        } else {
            gameData["restartBack"] && gameData["restartBack"].apply(this);
        }
        // this.game_btn.label="开始游戏";
        Laya.stage.mouseEnabled = true;
        this._isPlay = false;
        this.giveUp_btn.visible = false;
        // this.again_btn.visible=false;
        this._rightNums = 0;
        this.rotation_img.rotation = 0;
        this.boxRand();
        this.setGameState(2);
        this._gameState = 2;
        // this.nums_clip.index=this.numIndexArr[this._gameLevel];
        this.numTxt.text = "0";
        this.gameTime_txt.text = "我的最高分:" + gameData["total"];
    };
    GameUi.prototype.next = function () {
        this.boxRand();
        this.getArrow();
    };
    GameUi.prototype.boxRand = function () {
        this.box_img.rotation = Math.ceil(rand(10000) * 360);
        while (this.isInArea(this.box_img.rotation, this.rotation_img.rotation, 70)) {
            this.box_img.rotation = Math.ceil(rand(10000) * 360);
        }
    };
    GameUi.prototype.getArrow = function () {
        var intervalRange = this.box_img.rotation - this.rotation_img.rotation;
        if (intervalRange > 180) {
            this._targetValue = (intervalRange - 180) - 180;
        }
        else if (intervalRange < -180) {
            this._targetValue = intervalRange + 360;
        }
        else {
            this._targetValue = intervalRange;
        }
        Laya.timer.loop(30, this, this.tween);
    };
    GameUi.prototype.tween = function () {

        if (gameData["guide"] && this.isInArea(this.rotation_img.rotation, this.box_img.rotation, 5, 0)) {
            this._rightNums++;
            Laya.timer.clear(this, this.tween);
            this.showGuide();
            return;
        }
        if (this._targetValue > 0) {
            // this._deviation=4;
            if (this.isInArea(this.valueRange(this.rotation_img.rotation), this.valueRange(this.box_img.rotation + GameUi.Range + this.speed), this.speed)) {
                this.rotation_img.rotation = this.valueRange(this.box_img.rotation + GameUi.StopOver);
                this.stop();
            }
            else {
                this.rotation_img.rotation += this.speed;
            }
            this.rotation_img.rotation = this.valueRange(this.rotation_img.rotation);
        }
        else {
            // this._deviation=-4;
            if (this.isInArea(this.valueRange(this.rotation_img.rotation), this.valueRange(this.box_img.rotation - GameUi.Range - this.speed), this.speed)) {
                this.rotation_img.rotation = this.valueRange(this.box_img.rotation - GameUi.StopOver);
                this.stop();
            }
            else {
                this.rotation_img.rotation -= this.speed;
            }
            this.rotation_img.rotation = this.valueRange(this.rotation_img.rotation);
        }
    };
    GameUi.prototype.showGuide = function () {
        this.isInGuide = true;
        this.guide.play();
        this.guideNum++;
        this.guideNum > 1 && (this.numTxt.text = this._rightNums.toString());
        this.guide_box.visible = true;
        this.moduleBg.visible = true;
        this.guide_txt.text = this.guideLang[0][this.guideNum - 1];
        this.guide_btn.label = this.guideNum == 2 ? "" : "";
        Laya.stage.mouseEnabled = true;
    };
    GameUi.prototype.clickUi = function () {
        if (!this.canGame) {
            return;
        }
        if (this._gameState == 0) {
            return;
        }
        this.game_btn.visible = false;
        if (this.isInGuide)
            return;
        if (gameData["guide"]) {
            this.getArrow();
            Laya.stage.mouseEnabled = false;
            this.giveUp_btn.visible = false;
            // this.again_btn.visible=false;
            return;
        }
        if (!this._isPlay) {
            this._isPlay = true;
            this.getArrow();
            Laya.stage.mouseEnabled = true;
            this.giveUp_btn.visible = false;
            // this.again_btn.visible=false;
        }
        else {
            this.stop();
        }
    };
    GameUi.prototype.clickGuide = function (e) {
        if (!gameData["guide"]) {
            return;
        }
        e.stopPropagation();
        this.guide_box.visible = false;
        this.moduleBg.visible = false;
        this.isInGuide = false;

        if (this.guideNum == 1) {
            this.showGuide();
        }
        else if (this.guideNum == 2) {

            gameData["guide"] = 0;
            Laya.stage.off(Laya.Event.CLICK, this, this.clickGuide);
            this.top_img.visible = true;
            this.gameTime_txt.visible = true;
            this.restart();
            Laya.stage.on(Laya.Event.CLICK, this, this.clickUi);
        }
    };
    GameUi.prototype.stop = function (isDev) {
        if (isDev === void 0) {
            isDev = 0;
        }
        if (Date.now() - this._clickTime < 200) {
            this._clickTime = Date.now();
            return;
        }
        this._clickTime = Date.now();
        var self = this;
        Laya.timer.clear(this, this.tween);
        if (this._gameState == 0) {
            this.restart();
            this.gameTime++;
            // this.game_btn.visible=true;
        }
        else if (this.isInArea(this.rotation_img.rotation, this.box_img.rotation, GameUi.Range, isDev)) {
            this._rightNums++;
            this.numTxt.text = this._rightNums.toString();
            this.playSound("yes.mp3");
            this.next();
        }
        else {
            this.playSound("fail.mp3");
            gameData["resultBack"] && gameData["resultBack"].apply(this, [this._rightNums]);
            this._gameState = 0;
            this.setGameState(0);
            // Laya.timer.once(300,this,function(){Laya.stage.mouseEnabled=true;});
            this.giveUp_btn.visible = true;
            // this.again_btn.visible=true;
            this.game_btn.visible = false;
        }
    };
    /**
     * 设置界面状态
     * @param state 0错误 1正确 2正常
     */
    GameUi.prototype.setGameState = function (state) {
        for (var i = 0; this["num_bg_" + i]; i++) {
            if (i == state) {
                this["num_bg_" + i].visible = true;
            }
            else {
                this["num_bg_" + i].visible = false;
            }
        }

    };
    GameUi.prototype.valueRange = function (range) {
        if (range > 360) {
            range = range - 360;
        }
        else if (range < 0) {
            range = 360 + range;
        }
        return range;
    };
    /**
     * 是否在圆弧范围里面
     * @param value 目标值
     * @param target 中心值
     * @param range 范围
     */
    GameUi.prototype.isInArea = function (value, target, range, deviation) {
        if (deviation === void 0) {
            deviation = 0;
        }
        var min = this.valueRange(target - range + deviation);
        var max = this.valueRange(target + range + deviation);
        if (min < 360 && max > 0 && min > max) {
            if (value <= 360 && value >= min) {
                return true;
            }
            else if (value >= 0 && value <= max) {
                return true;
            }
        }
        else {
            if (value >= min && value <= max) {
                return true;
            }
        }
        return false;
    };
    GameUi.prototype.giveUp = function (e) {
        e.stopPropagation();
    };
    Object.defineProperty(GameUi.prototype, "gameTime", {
        get: function () {
            return gameData["GameNums"];
        },
        set: function (value) {
            gameData["GameNums"] = value;
            this.gameTime_txt.text = value.toString();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(GameUi.prototype, "getInitInfo", {
        get: function () {
            return window["getParam"]();
        },
        enumerable: true,
        configurable: true
    });
    GameUi.prototype.playSound = function (soundName) {
        Laya.SoundManager.playSound("res/sound/" + soundName);
    };
    GameUi.Range = 14;
    GameUi.StopOver = 16;
    GameUi.guideTotal = 3;
    return GameUi;
}(ui.lock.lockUI));
rnd['today'] = new Date();
rnd['seed'] = rnd['today'].getTime();

function rnd() {
    rnd['seed'] = (rnd['seed'] * 9301 + 49297) % 233280;
    return rnd['seed'] / (233280.0);
}
;

function rand(number) {
    return Math.ceil(rnd() * number) / number;
}
;
//程序入口
Laya.init(750, 1624);
Laya.stage.scaleMode = Laya.Stage.SCALE_FIXED_HEIGHT;
Laya.stage.alignH = Laya.Stage.ALIGN_CENTER;
Laya.stage.alignV = Laya.Stage.ALIGN_MIDDLE;


function beginGame() {
    gameData["guide"] = arguments[0];
    gameData["speed"] = arguments[1];
    gameData["GameNums"] = arguments[2];
    gameData["total"] = arguments[3]; //历史最高分
    gameData["resultBack"] = arguments[4]; //结果返回
    gameData["restartBack"] = arguments[5]; //重开前返回
    var reload = ["comp/bg0.jpg", "comp/bg1.jpg", "comp/bg2.jpg", "comp/clip_num.png", "comp/diban.png", "res/atlas/comp.atlas", "comp/guideBtn.png"];
    Laya.loader.load(reload, Handler.create(null, onLoaded));
}

function onLoaded() {
    //实例UI界面
    var gameUi = new GameUi();
    Laya.stage.addChild(gameUi);
    gameUi.startGame();
}